package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session){
        //获取登录人
        Long id = (Long) session.getAttribute("employee");
        //完善套餐数据
        setmealDto.setCreateUser(id);
        setmealDto.setUpdateUser(id);
        setmealService.save(setmealDto);
        return R.success("添加成功");

    }

    //处理套餐页面的请求
    @GetMapping("/page")
    public R<Page<SetmealDto>> findByPage(Integer page, Integer pageSize ,String name){
        Page<SetmealDto> pageObj = setmealService.findByPage(page, pageSize, name);
        return R.success(pageObj);
    }

    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids, HttpSession session){
       Long id = (Long) session.getAttribute("employee");
       setmealService.deleteByid(ids,id);
       return R.success("删除套餐成功");
    }


}
